//package com.ruoyi.framework.shiro.web.filter.captcha;
//
//import com.anji.captcha.model.vo.CaptchaVO;
//import com.anji.captcha.service.CaptchaService;
//import com.anji.captcha.service.impl.DefaultCaptchaServiceImpl;
//import com.ruoyi.common.constant.ShiroConstants;
//import com.ruoyi.common.utils.StringUtils;
//import org.apache.shiro.web.filter.AccessControlFilter;
//
//import javax.servlet.ServletRequest;
//import javax.servlet.ServletResponse;
//import javax.servlet.http.HttpServletRequest;
//
///**
// * 验证码过滤器
// *
// * @author ruoyi
// */
//public class CaptchaValidateFilterWithAjcaptcha extends AccessControlFilter {
//    /**
//     * 是否开启验证码
//     */
//    private boolean captchaEnabled = true;
//    /**
//     * 验证码类型
//     */
//    private String captchaType = "math";
//
//    private CaptchaService captchaService;
//
//    public void setCaptchaEnabled(boolean captchaEnabled) {
//        this.captchaEnabled = captchaEnabled;
//    }
//
//    public void setCaptchaType(String captchaType) {
//        this.captchaType = captchaType;
//    }
//
//    @Override
//    public boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
//        request.setAttribute(ShiroConstants.CURRENT_ENABLED, captchaEnabled);
//        request.setAttribute(ShiroConstants.CURRENT_TYPE, captchaType);
//        return super.onPreHandle(request, response, mappedValue);
//    }
//
//    @Override
//    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue)
//            throws Exception {
//        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
//        // 验证码禁用 或不是表单提交 允许访问
//        if (captchaEnabled == false || !"post".equals(httpServletRequest.getMethod().toLowerCase())) {
//            return true;
//        }
//        return validateResponse(httpServletRequest, httpServletRequest.getParameter("__captchaVerification"));
//    }
//
//    public boolean validateResponse(HttpServletRequest request, String captchaVerification) {
////        Object obj = ShiroUtils.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
////        String code = String.valueOf(obj != null ? obj : "");
////        // 验证码清除，防止多次使用。
////        request.getSession().removeAttribute(Constants.KAPTCHA_SESSION_KEY);
////        if (StringUtils.isEmpty(validateCode) || !validateCode.equalsIgnoreCase(code)) {
////            return false;
////        }
//        CaptchaVO captchaVO = new CaptchaVO();
//        captchaVO.setCaptchaVerification(captchaVerification);
//        if(captchaService instanceof DefaultCaptchaServiceImpl){
//            captchaVO.setCaptchaType(request.getParameter("captchaType"));
//        }
//        if (StringUtils.isEmpty(captchaVerification) || !captchaService.verification(captchaVO).isSuccess()) {
//            return false;
//        }
////        int i =1/0;
//        return true;
//    }
//
//    @Override
//    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
//        request.setAttribute(ShiroConstants.CURRENT_CAPTCHA, ShiroConstants.CAPTCHA_ERROR);
//        return true;
//    }
//
//    public void setCaptchaService(CaptchaService captchaService) {
//        this.captchaService = captchaService;
//    }
//}